package com.longge;

public class BinarySearch {

    public int search(int[] nums, int target) {

        int left = 0;
        int right = nums.length;

        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] == target) {
                return mid;
            } else if (nums[mid] < target) {
                left = mid + 1;
            } else if (nums[mid] > target) {
                right = mid;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        new BinarySearch().search(new int[]{-1, 0, 3, 5, 9, 12}, 9);
    }
}
